package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.GroupLinkManAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.GroupLinkManBean;
import com.yanchangapp.ui.presenter.LinkManAtPresenter;
import com.yanchangapp.ui.view.LinkManAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.KeyboardHelper;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * page: 关联员工 -->
 * 这里有一个类别的分组....
 * <p>
 * 可点击具体员工进入员工的工作项
 */
public class LinkManActivity extends BaseActivity<LinkManAtView, LinkManAtPresenter> implements LinkManAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.rvLinkMan)
    RecyclerView rvLinkMan;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.llEmptyMsg)
    LinearLayout llEmptyMsg;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    @BindView(R.id.etSearchText)
    EditText etSearchText;
    @BindView(R.id.ivDelete)
    ImageView ivDelete;
    @BindView(R.id.tvCancel)
    TextView tvCancel;
    @BindView(R.id.tvHeadDes)
    TextView tvHeadDes;
    @BindView(R.id.rlHead)
    RelativeLayout rlHead;
    @BindView(R.id.ivDel)
    ImageView ivDel;
    private List<GroupLinkManBean> mGroupLinkManBeans = new ArrayList<>();
    private GroupLinkManAdapter mGroupLinkManAdapter;
    // 关键词
    private String mCurrentKeywords = null;
    // 判断是否已经加载成功...
    private boolean hasLoadinSuccess = false;

    private void setLoadingState(LoadingType loadingType) {
        switch (loadingType) {
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE) {
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE) {
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE) {
                    refreshLayout.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.GONE) {
                    llEmptyMsg.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE) {
                    refreshLayout.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE) {
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.GONE) {
                    refreshLayout.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE) {
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE) {
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE) {
                    refreshLayout.setVisibility(View.GONE);
                }
                break;
        }
    }

    public static void show(Activity activity) {
        Intent intent = new Intent(activity, LinkManActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_link_man;
    }

    @Override
    protected LinkManAtPresenter createPresenter() {
        return new LinkManAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("关联员工");
        tvDes.setText("暂无关联员工");
        initHead();
        initAdapter();
    }

    private void initHead() {
        tvHeadDes.setText("说明：用于查看所有你可以指派的员工");
    }

    private void initAdapter() {
        rvLinkMan.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvLinkMan.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this, 0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
        mGroupLinkManAdapter = new GroupLinkManAdapter(mGroupLinkManBeans);
        rvLinkMan.setAdapter(mGroupLinkManAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryLinkManLists(mCurrentKeywords);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rlHead.setVisibility(View.GONE);
            }
        });

        tvCancel.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(etSearchText.getText().toString())){
                etSearchText.setText("");
                KeyboardHelper.hideKeyboard(tvCancel);
            }

        });

        ivDelete.setOnClickListener(v -> {
            etSearchText.setText("");
            KeyboardHelper.hideKeyboard(tvCancel);
        });


        mGroupLinkManAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.llItem){
                    LookLinkWorkItemActivity.show(LinkManActivity.this,mGroupLinkManBeans.get(position).getId());
                }
            }
        });

        etSearchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            /**
             * 当搜索框的文字变化时回调此方法
             * @param s
             * @param start
             * @param before
             * @param count
             */
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mCurrentKeywords = s.toString();
                if (s.toString().length() == 0) {
                    ivDelete.setVisibility(View.GONE);
                } else {
                    ivDelete.setVisibility(View.VISIBLE);
                }
                refreshData();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        btnNextRefresh.setOnClickListener(v -> initData());
    }

    /**
     * 进行数据刷新
     */
    private void refreshData() {
        initData();
    }

    @Override
    public void queryGroupLinkManSuccess(List<GroupLinkManBean> lists) {
        if (lists.size() == 0) {
            setLoadingState(LoadingType.EMPTY);
        } else {
            setLoadingState(LoadingType.SUCCESS);
            mGroupLinkManBeans.clear();
            mGroupLinkManBeans.addAll(lists);
            mGroupLinkManAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void queryGroupLinkMainFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }
}
